/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.appdict;

import co.yixiang.dozer.service.IGenerator;
import co.yixiang.enums.CommonEnum;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.appdict.domain.Appdict;
import co.yixiang.modules.appdict.service.AppdictService;
import co.yixiang.modules.appdict.service.dto.AppdictDto;
import co.yixiang.modules.appdict.service.dto.AppdictQueryCriteria;
import co.yixiang.modules.shop.service.YxSystemGroupDataService;
import co.yixiang.utils.RedisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
* @author yhh
* @date 2020-09-08
*/
@AllArgsConstructor
@Api(tags = "Appdict管理")
@RestController
@RequestMapping("/api/yxAppdict")
public class YxAppdictController {

    private final AppdictService yxAppdictService;
    private final YxSystemGroupDataService systemGroupDataService;
    private final RedisUtils redisUtils;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','yxAppdict:list')")
    public void download(HttpServletResponse response, AppdictQueryCriteria criteria) throws IOException {
        yxAppdictService.download(generator.convert(yxAppdictService.queryAll(criteria), AppdictDto.class), response);
    }

    @GetMapping
    @Log("查询Appdict")
    @ApiOperation("查询Appdict")
    @PreAuthorize("@el.check('admin','yxAppdict:list')")
    public ResponseEntity<Object> getYxAppdicts(AppdictQueryCriteria criteria, Pageable pageable){
        List<Appdict> appdictList = yxAppdictService.queryAll(criteria);
        List<AppdictDto> appdictDtoList = generator.convert(appdictList, AppdictDto.class);

        return new ResponseEntity<>(yxAppdictService.buildTree(appdictDtoList),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增Appdict")
    @ApiOperation("新增Appdict")
    @PreAuthorize("@el.check('admin','yxAppdict:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Appdict resources){
        redisUtils.set("dict:"+resources.getCode() , resources.getMemo());
        return new ResponseEntity<>(yxAppdictService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改Appdict")
    @ApiOperation("修改Appdict")
    @PreAuthorize("@el.check('admin','yxAppdict:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Appdict resources){
        redisUtils.set("dict:"+resources.getCode() , resources.getMemo());
        yxAppdictService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除Appdict")
    @ApiOperation("删除Appdict")
    @PreAuthorize("@el.check('admin','yxAppdict:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody String[] ids) {
        Arrays.asList(ids).forEach(id->{
            yxAppdictService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("获取下级dict")
    @ApiOperation("获取下级dict")
    @GetMapping("/getDictById")
    public List<Appdict> getDictById(@RequestParam(value = "oid" ,required = false) String oid) {
        return yxAppdictService.list(new QueryWrapper<Appdict>().lambda()
                .eq(Appdict::getParentOid, oid)
                .eq(Appdict::getIsDelete, CommonEnum.DEL_STATUS_0.getValue()));
    }

//    @Log("获取菜单专区")
//    @ApiOperation("获取菜单专区")
//    @GetMapping("/getMenuByName")
//    public List<JSONObject> getMenuByName(@RequestParam(value = "oid" ,required = false) String oid) {
//        Long[] l1 = new Long[]{197L , 196L , 199L , 200L , 209L , 227L , 228L};
//        List<YxSystemGroupData> list = systemGroupDataService.list(new QueryWrapper<YxSystemGroupData>().lambda()
//                .eq(YxSystemGroupData::getGroupName, ShopConstants.YSHOP_MY_MENUES)
//                .notIn(YxSystemGroupData::getId, l1)
//                .orderByDesc(YxSystemGroupData::getSort));
//        List<JSONObject> s1 = list
//                .stream()
//                .map(YxSystemGroupData::getValue)
//                .map(JSONObject::parseObject)
//                .collect(Collectors.toList());
//        return s1;
//    }
}
